using SqlSugar;
using System;
using System.Linq; 

namespace WebSelfHost.Logic.Base
{
    /// <summary>
    /// 数据库访问基类
    /// </summary>
    public class BaseLogic
    {
        public static string MySQLConnString { get; set; }
        public static bool IsDebug { get; set; }
        /// <summary>
        /// 获得数据库连接实例
        /// </summary>
        /// <returns></returns>
        public static SqlSugarClient GetInstance()
        {
            SqlSugarClient db = null;
            db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = MySQLConnString,
                DbType = DbType.MySql,
                IsAutoCloseConnection = true
            });

            //用来打印Sql方便你调式    
            db.Aop.OnLogExecuting = (sql, pars) =>
            {
                if (IsDebug)
                {
                    Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                    Console.WriteLine();
                }
            };
            return db;
        }
    }
}
